package com.example.smartwaterapi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池配置类
 * 优化版本 - 适合4核8G环境，支持500并发
 */
@Configuration
@EnableAsync
public class ThreadPoolConfig {

    /**
     * 核心线程池大小
     */
    private static final int CORE_POOL_SIZE = 50;  // 提高到50

    /**
     * 最大线程池大小
     */
    private static final int MAX_POOL_SIZE = 100; // 提高到100

    /**
     * 队列容量
     */
    private static final int QUEUE_CAPACITY = 1000; // 提高到1000

    /**
     * 线程池维护线程所允许的空闲时间
     */
    private static final int KEEP_ALIVE_SECONDS = 60;

    /**
     * 配置通用异步任务线程池
     */
    @Bean("asyncTaskExecutor")
    public ThreadPoolTaskExecutor asyncTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setQueueCapacity(QUEUE_CAPACITY);
        executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
        executor.setThreadNamePrefix("async-task-");
        
        // 线程池对拒绝任务的处理策略：由调用线程处理（一种退化方式）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        // 允许核心线程超时，有助于释放资源
        executor.setAllowCoreThreadTimeOut(true);
        
        // 初始化
        executor.initialize();
        return executor;
    }
    
    /**
     * 配置数据处理专用线程池
     */
    @Bean("dataProcessExecutor")
    public ThreadPoolTaskExecutor dataProcessExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(25);    // 提高到25
        executor.setMaxPoolSize(50);     // 提高到50
        executor.setQueueCapacity(500);  // 提高到500
        executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
        executor.setThreadNamePrefix("data-process-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setAllowCoreThreadTimeOut(true);
        executor.initialize();
        return executor;
    }
    
    /**
     * 配置IO密集型任务线程池
     */
    @Bean("ioTaskExecutor")
    public ThreadPoolTaskExecutor ioTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // IO密集型任务线程池参数优化
        int processors = Runtime.getRuntime().availableProcessors();
        executor.setCorePoolSize(processors * 4);  // CPU核心数*4
        executor.setMaxPoolSize(processors * 8);   // CPU核心数*8
        executor.setQueueCapacity(500);            // 提高到500
        executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
        executor.setThreadNamePrefix("io-task-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setAllowCoreThreadTimeOut(true);
        executor.initialize();
        return executor;
    }
    
    /**
     * 配置轻量级任务线程池
     * 适用于大量简短、轻量级任务
     */
    @Bean("lightTaskExecutor")
    public ThreadPoolTaskExecutor lightTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 轻量任务可以使用较少线程
        executor.setCorePoolSize(10);    // 提高到10
        executor.setMaxPoolSize(20);     // 提高到20
        executor.setQueueCapacity(200);  // 提高到200
        executor.setKeepAliveSeconds(30); // 较短的存活时间
        executor.setThreadNamePrefix("light-task-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setAllowCoreThreadTimeOut(true);
        executor.initialize();
        return executor;
    }
    
    /**
     * 配置定时任务线程池
     */
    @Bean("scheduledTaskExecutor") 
    public ThreadPoolTaskExecutor scheduledTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(8);  // 提高到8
        executor.setMaxPoolSize(16);  // 提高到16
        executor.setQueueCapacity(100); // 提高到100
        executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
        executor.setThreadNamePrefix("scheduled-task-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setAllowCoreThreadTimeOut(true);
        executor.initialize();
        return executor;
    }
} 